package class03_一些基础的数据结构.test;

public class Code04_RingArray {
    public static class myQueue{
        public int[] arr;
        public int pushi;
        public int polli;
        public int size;
        public final int limit;
        public myQueue(int limit){
            arr = new int[limit];
            pushi = 0;
            polli = 0;
            size = 0;
            this.limit = limit;
        }
        public void push(int val){
            if(size == limit){
                throw new RuntimeException("队列满了，不能再加了");
            }
            size++;
            arr[pushi] = val;
            pushi = nextIndex(pushi);
        }

        public Integer poll(){
            if(size == 0){
                return null;
            }
            size --;
            int res = arr[polli];
            polli = nextIndex(polli);
            return res;
        }

        private int nextIndex(int pushi) {
            return pushi < limit -1 ? pushi + 1 : 0;
        }
    }
}
